<?
class user_list{

	private $items;
	private $itemsPerPage;
	private $pages;
	private $currentPage;
	protected $html;
	private $pageUrl;
	protected $url;
	protected $firstRow;
	
	public function __construct(array $iItems, $iItemsPerPage = NULL){
	
		$this -> items = $iItems;
		$this -> html = new view_htmlTag('table');
		$this -> html -> setAttribute('cellspacing', '0');
		$this -> html -> setAttribute('cellpadding', '0');

		$this -> html -> setAttribute('class', 'bewGroep_table');
		$heading1 = new view_htmlTag('th');
		$heading1 -> setText('Bewonersgroep:');
		$heading1 -> setAttribute('id', 'name');
		$heading2 = new view_htmlTag('th');
		$heading2 -> setText('Locatie:');
		$this -> firstRow = new view_htmlTag('tr');
		
		$this -> firstRow -> addChild($heading1);
		$this -> firstRow -> addChild($heading2);
		
		$this -> html -> addChild($this -> firstRow);
		
		
		
		$this -> itemsPerPage = $iItemsPerPage;
		
		if(is_array($this -> items) && count($this -> items) > 0){
	
			$itemsAmount = count($this -> items);
			
			if($this -> itemsPerPage == NULL){
				$this -> pages = 1;
			}else{
			
			$this -> pages = ceil($itemsAmount / $this -> itemsPerPage);
			}
		
		}
		
		$this -> config();
	
	}
	
	
	protected function config(){}

	
	public function setPageUrl($iUrl){
	
		$this -> pageUrl = $iUrl;
	
	}
	
	
	
	
	public function renderPage($i){
	
		if($i < 0 || $i > $this -> pages - 1){
		
			$i = 0;
			
		
		}
		
		
		$this -> currentPage = $i;
		
		$startIndex = $i * $this -> itemsPerPage;
		
		if($this -> itemsPerPage == NULL){
			$stopIndex = count($this -> items) - 1;
		}else{
		$stopIndex = $startIndex + $this -> itemsPerPage;
			
			if($stopIndex >= count($this -> items)){
			
				$stopIndex = count($this -> items) - 1;
			
			}
			
		}
			return $this -> renderNextPage($startIndex, $stopIndex);
	
	}
	
	public function setUrl($iUrl){
	
		$this -> url = $iUrl;
	
	}
	
	
	
	private function renderNextPage($start, $stop){
		
		
		
		for($i = $start; $i <= $stop; $i++){
		
			$this -> html -> addChild($this -> renderRow($this -> items[$i]));
		
		}
		
		return $this -> html -> render();
	
	}
	
	public function getNextPage(){
		
		 if($this -> currentPage + 1 <= $this -> pages - 1){
			
		 	return $this -> pageUrl . '/' . ($this -> currentPage + 1);
		 
		 }else{
		 	return NULL;
		 }
	
	}
	
	
	public function getPrevPage(){
	
		 if($this -> currentPage - 1 >= 0){
			
		 	return $this -> pageUrl . '/' . ($this -> currentPage - 1);
		
		 
		 }else{
		 	return NULL;
		 }
	
	}
	
	protected function renderRow($item){
	
		
		$row = new view_htmlTag('tr');
		
		$titel = new view_htmlTag('td');
		$loc = new view_htmlTag('td');
		
		$loc -> setText($item -> locatie);
		$titel -> setText('<a href ="'.$this -> url.'/'.view::encodeForUrl($item -> titel).'">'. $item -> titel . '</a>');
		
		$row -> addChild($titel);
		$row -> addChild($loc);
		
				
		return $row;
		
		
		
	
	
	}






}




?>